<?php

/**
* ShowRouteTest.php
*
* Show route test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../ShowRoute.class.php';

class  ShowRouteTest extends WebTestCase
{
    private $testName = array();
    private $idArray = array();
    private $testDesc;

    public function setUp() 
    {

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $this->url = $global['service_url'];

        $this->testName[] = md5(uniqid(rand(), true));
        $this->testDesc = md5(uniqid(rand(), true));        
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("INSERT INTO guide (name,desc) values ( '".$this->testName[0]."','".$this->testDesc."' )");
        $this->idArray['guide_id'] = $dbh->lastInsertId();
        $dbh->exec("UPDATE guide SET  pic_file = '".$this->idArray['guide_id']."images.jpg' WHERE rowid=".$this->idArray['guide_id']);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$this->idArray['guide_id'].'images.jpg');
        $dbh->exec("UPDATE guide SET  sound_file = '".$this->idArray['guide_id']."sound.wav' WHERE rowid=".$this->idArray['guide_id']);
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$this->idArray['guide_id'].'sound.wav');

        $dbh->exec("INSERT INTO slide (guide_id,name,`order`) values ( '".$this->idArray['guide_id']."', '".$this->testName[1]."',0 )");
        $this->idArray['slide_1'] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide (guide_id,name,`order`) values (  '".$this->idArray['guide_id']."', '".$this->testName[2]."',2 )");
        $this->idArray['slide_2'] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide (guide_id,name,`order`) values (  '".$this->idArray['guide_id']."', '".$this->testName[3]."',1 )");
        $this->idArray['slide_3'] = $dbh->lastInsertId();
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");        
        $this->get($this->url.'?cmd=ShowRoute&id='.$this->idArray['guide_id']);
        
    }

    public function testGuideList()
    {

        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty("CONFIG_INI","GLOBAL");        
        $this->assertTitle('Wirtualny Przewodnik');
        $this->assertText($this->testName[0]);
        $this->assertText($this->idArray['guide_id'].'images.jpg');
        $this->assertPattern('/send_image.php\?id='.$this->idArray['guide_id'].'images.jpg/');
        $this->assertText($this->testDesc);        
        $this->assertLink('Opis dźwiękowy',$global['service_url'].'send_sound.php?id='.$this->idArray['guide_id'].'sound.wav'); 
         
        $this->assertLink($this->testName[1],$global['service_url']."index.php?cmd=ShowSlide&id=".$this->idArray['slide_1']."&guide_id=".$this->idArray['guide_id'],$global['service_url']."index.php?cmd=ShowSlide&id=".$this->idArray['slide_1']."&guide_id=".$this->idArray['guide_id']);
        $this->assertLink($this->testName[2],$global['service_url']."index.php?cmd=ShowSlide&id=".$this->idArray['slide_2']."&guide_id=".$this->idArray['guide_id']);
        $this->assertLink($this->testName[3],$global['service_url']."index.php?cmd=ShowSlide&id=".$this->idArray['slide_3']."&guide_id=".$this->idArray['guide_id']);
        
    }

    
    public function testSlideOrder()
    {

        $this->assertPattern('/.*'.$this->testName[1].'[\s\S]*'.$this->testName[3].'[\s\S]*'.$this->testName[2].'.*/');
        
    }  
    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $sth = $dbh->prepare("SELECT pic_file,sound_file FROM guide WHERE rowid = ".$this->idArray['guide_id']);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        
        
        if ( is_file($upload['upload_dir'].$result['pic_file']) )
        {
            unlink($upload['upload_dir'].$result['pic_file']);
        }

        if ( is_file($upload['upload_dir'].$result['sound_file']) )
        {
            unlink($upload['upload_dir'].$result['sound_file']);
        }

        
        $dbh->exec("DELETE FROM guide WHERE rowid = ".$this->idArray['guide_id']);
        $dbh->exec("DELETE FROM slide WHERE rowid = ".$this->idArray['slide_1']);
        $dbh->exec("DELETE FROM slide WHERE rowid = ".$this->idArray['slide_2']);
        $dbh->exec("DELETE FROM slide WHERE rowid = ".$this->idArray['slide_3']);
        $dbh = null;
    }
    
}
?>
